package com.qingke;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ServletsTest2
 */
@WebServlet(
		//任意以.ServletsTest2的都可以到该网页
		urlPatterns={"*.ServletsTest2"},
		initParams={
				@WebInitParam(name="config1",value="/first"),
				@WebInitParam(name="config2",value="/second")
		}
)

public class ServletsTest2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ServletConfig config;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletsTest2() {
        super();
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
//		this.config=config;
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Servlet#getServletConfig()
	 */
//	public ServletConfig getServletConfig() {
//		return config;
//	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Enumeration<String> enums = config.getInitParameterNames();
//		while(enums.hasMoreElements()){
//			String name = enums.nextElement();
//			response.getWriter().append("name"+config.getInitParameter(name));
//			response.getWriter().append("<br>");
//			
//		}		
//		
//		response.getWriter().append("Served at: ").append(request.getContextPath()).append("<br>");
//		response.getWriter().append("Req CHarEncoding: ").append(request.getCharacterEncoding()).append("<br>");
//		response.getWriter().append("Req Content Type: ").append(request.getContentType()).append("<br>");
//		response.getWriter().append("Eeq Content Type:　").append(String.valueOf(request.getContentType())).append("<br>");
//		response.getWriter().append("Req Type:　").append(request.getClass().getName()).append("<br>");
//		response.getWriter().append("======================");
		
		
		//localhost:8080/Servlets/ServletsTest2?id=11&name="YaDan"&age="99" 得到后面的键值对
		Enumeration<String> enumsParam = request.getParameterNames();
		while (enumsParam.hasMoreElements()) {
			String name = enumsParam.nextElement();
			String value = request.getParameter(name);
			response.getWriter().append(name).append(" = "+value).append("<br>");
			System.out.println(name+" = "+value);
		}
		
//		//读本地图片
//		doGet(request, response);
		
//		//重定向
//		response.sendRedirect("http://www.baidu.com");
//		System.out.println("重定向");
		//自定义错误页面
//		response.sendError(404);
		response.getWriter().append("null pointer");
		throw new NullPointerException("null pointer");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath()).append("\r\n");
//		response.getWriter().append("Req CHarEncoding: ").append(request.getCharacterEncoding()).append("\r\n");
//		response.getWriter().append("Req Content Type: ").append(request.getContentType()).append("\r\n");
//		response.getWriter().append("Eeq Content Type:　").append(String.valueOf(request.getContentType())).append("\r\n");
//		response.getWriter().append("Req Type:　").append(request.getClass().getName()).append("\r\n");

		//本地读图
		response.setContentType("image/png");
		String file = "D:/img/img3.png";
		FileInputStream fis = new FileInputStream(new File(file));
		int length = fis.available();
		byte[] content = new byte[length];
		fis.read(content);
		fis.close();
		response.getOutputStream().write(content);
		response.setContentLength(length);
		response.flushBuffer();
		
//		response.setContentType("text/plain; charset=utf-8");
//		response.getWriter().append("手持鸡腿的围观群众");
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
